<?php
class AdminController extends GxController
{
    public $baseUrl;
	public $layout = '//layouts/tutuquan';

    protected function beforeAction($action) {
        $assets = Yii::getPathOfAlias('static').DIRECTORY_SEPARATOR.'backend'.DIRECTORY_SEPARATOR;
        //在asset中建立一个不冲突文件夹
        $this->baseUrl = Yii::app()->assetManager->publish($assets, false, -1, YII_DEBUG);
        if (Yii::app()->user->isGuest)
        {
            //重定向到登录页面
            Yii::app()->user->loginRequired();
        }else{
            if(Yii::app()->user->getState('userSessionTimeout') < time()){
                // timeout 如果过期执行logout方法
                Yii::app()->user->logout();
                Yii::app()->user->loginRequired();
            }else{
                Yii::app()->user->setState('userSessionTimeout', time() + Yii::app()->params['sessionTimeoutSeconds']) ;
            }
        }
        return true;
    }


	public function init (){
//        parent::init();
//		$this->_gets = Yii::app()->request;
//		$this->_xsession = new CHttpSession();
//        $this->_xsession->open();
//        if (isset($_POST['sessionId'])) {
//            $session = Yii::app()->getSession();
//            $session->close();
//            $session->sessionID = $_POST['sessionId'];
//            $session->open();
//        }
//		$this->_adminUserId = Yii::app()->user->id;
//		$this->front = Yii::app()->baseUrl;
	}














    /**
     * 权限检测
     * 超级用户组跳过检测
     * 附加 index_index 后台首页,防止重复验证权限
     * @param $action
     */

    protected function _acl ($action = false, $params = array('response'=>false, 'append'=>',default_index,default_home'))
    {
        //$this->id 控制器名称    $this->action->id  方法名称   manage_ranklist ===>权限文件名称
        $actionFormat = empty($action) ? strtolower($this->id . '_' . $this->action->id) : strtolower($action);

            //这里又牵涉到登陆后的session问题 以后再处理
//        $permission = self::_sessionGet('_adminiPermission');
//        if ($permission != 'administrator') {
//            $adminiGroup = self::_sessionGet('_adminiGroupId');
//            $aclDb = AdminGroup::model()->findByPk($adminiGroup);
//            try {
//                if (! in_array($actionFormat, explode(',', strtolower($aclDb->acl) . $params['append'])))
//                    throw new Exception('当前角色组无权限进行此操作，请联系管理员授权');
//            } catch (Exception $e) {
//                if($params['response'] == 'text'){
//                    exit($e->getMessage());
//                }elseif($params['response'] == 'json'){
//                    $var['state'] = 'error';
//                    $var['message'] = $e->getMessage();
//                    exit(CJSON::encode($var));
//                }else{
//                    $referrer = Yii::app()->request->urlReferrer? Yii::app()->request->urlReferrer : $this->createUrl('default/home') ;
//                    if(preg_match("/default\/index/i", $referrer))
//                        $referrer =  $this->createUrl('default/home') ;
//                    $tplVar = array(
//                        'code'=>'访问受限',
//                        'message'=>$e->getMessage(),
//                        'redirect'=>$params['redirect'] ? $params['redirect'] : $referrer ,
//                    );
//                    exit($this->render('/_include/_error', $tplVar));
//                }
//            }
//        }
    }


    protected function ajaxReturn($data,$type='') {
        if(empty($type)) $type  =   Yii::app()->params['DEFAULT_AJAX_RETURN'];
        switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
            case 'JSONP':
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                $handler  =   isset($_GET[Yii::app()->params['VAR_JSONP_HANDLER']]) ? $_GET[Yii::app()->params['VAR_JSONP_HANDLER']] : Yii::app()->params['DEFAULT_JSONP_HANDLER'];
                exit($handler.'('.json_encode($data).');');
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($data);
        }
    }
	
}